package com.sotb.receiver;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 
 * @author 永能
 *
 */
public class HistoryDbUtil {
	private int idExist = -1;
	private SQLiteDatabase db;
	public HistoryDbUtil(SQLiteDatabase db){
		this.db = db;
	}
	
	//插入数据
	public void insertData(String number, String location){
		
	    if(isNumberExist(number)){
	    	updateById(location);
	    }
	    else{
	    	try{
	    		Date now = new Date();
	    		DateFormat d2 = DateFormat.getDateTimeInstance();
	    	    String time = d2.format(now);
	    		ContentValues values = new ContentValues();
	    		values.put("number", number);
	    		values.put("time", time);
	    		values.put("location", location);
	    		values.put("isCompleted", Constant.RECEIVE_NOT_COMPLETE);
	    		db.insert("history_list", null, values);
	    	
	    		/*
	    		db.execSQL("insert into history_list(number, time, location, isCompleted) " +
	    				"values(" + number + "," + time + "," + location + "," + isCompleted + ")");
	    		 */
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		System.out.println("insert wrong");
	    	}
	    }
	}
	
	//更新接警状态
	public void updateReceiveComplete(String number){
		ContentValues values = new ContentValues();
    	/*
    	values.put("number", number);
    	values.put("time", time);
    	values.put("location", location);
    	*/
		values.put("isCompleted", Constant.RECEIVE_COMPLETE);
		db.update("history_list", values, "number=?", new String[]{number});
		
	}
	
	//判断号码是否存在
	public boolean isNumberExist(String number){
		try{
			Cursor cursor = db.query("history_list", new String[]{"id", "number", "time", "location", "isCompleted"}, null, null, null, null, null);
			cursor.moveToNext();
			while(cursor != null ){
				String numberTemp = cursor.getString(cursor.getColumnIndex("number"));
				if( numberTemp.equals(number)){
					idExist = cursor.getInt(cursor.getColumnIndex("id"));
					return true;
				}
				cursor.moveToNext();
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	//根据相同号码得到的id来更新坐标
	public void updateById(String location){
		try{
			Cursor cursor = db.query("history_list", new String[]{"id", "number", "time", "location", "isCompleted"}, "id=?", new String[]{idExist+""}, null, null, null);
			cursor.moveToNext();
			String locationTemp = cursor.getString(cursor.getColumnIndex("location"));
			locationTemp += (" " + location);
			ContentValues values = new ContentValues();
			values.put("location", locationTemp);
			db.update("history_list", values, "id=?", new String[]{idExist+""});
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	public List<String[]> ReceiveNotCompleteInFo(){
		//String[][] info = new String[1][];
		List<String[]> infos = new ArrayList<String[]>();
		
		try{
			Cursor cursor = db.query("history_list", new String[]{"id", "number", "time", "location", "isCompleted"}, "isCompleted=?", new String[]{"0"}, null, null, null);
			cursor.moveToNext();
			
			while(cursor != null){
				String number = cursor.getString(cursor.getColumnIndex("number"));
				String location = cursor.getString(cursor.getColumnIndex("location"));
				String[] locationSep = location.split(" ");
				String locationLast = locationSep[locationSep.length-1];
				String[] locationLastSpe = locationLast.split(",");
				String locationLastLongitude = locationLastSpe[0];
				String locationLastLatitude = locationLastSpe[1];
				String[] info = new String[3];
				info[0] = locationLastLongitude;
				info[1] = locationLastLatitude;
				info[2] = number;
			    infos.add(info);
			    
				cursor.moveToNext();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return infos;
	}

}
